package com.sg.business.commons.editingsupport;

import java.util.Iterator;

import com.sg.business.model.TaskForm;
import com.sg.business.model.UserTask;
import com.sg.business.resource.nls.Messages;
import com.sg.widgets.MessageUtil;
import com.sg.widgets.Widgets;
import com.sg.widgets.commons.editingsupport.CellClickListener;
import com.sg.widgets.part.editor.DataObjectDialog;
import com.sg.widgets.registry.config.DataEditorConfigurator;

public class WorkflowHistoryDescEditingSupport extends CellClickListener {

	@Override
	protected void selected(Object element, String columnId) {
		if (element instanceof UserTask) {
			doOpenTaskForm((UserTask) element);
		}
	}

	private void doOpenTaskForm(UserTask userTask) {
		String editorId = (String) userTask.getStringValue(TaskForm.F_EDITOR);
		if (editorId == null) {
			return;
		}
		TaskForm taskForm = userTask.makeTaskForm();

		Iterator<String> iter = userTask.get_data().keySet().iterator();
		while (iter.hasNext()) {
			String key = iter.next();
			if (key.startsWith("form_")) { //$NON-NLS-1$
				String nkey = key.substring(5);
				taskForm.setValue(nkey, userTask.getValue(key));
			}
		}
		DataEditorConfigurator ec = (DataEditorConfigurator) Widgets
				.getEditorRegistry().getConfigurator(editorId);
		try {
			DataObjectDialog.openDialog(taskForm, ec, false, null,
					Messages.get().ProcessHistoryUIToolkit_5);
		} catch (Exception e) {
			MessageUtil.showToast(e);
		}
	}
}
